package com.legendframework.core.ioc.impl;

import com.legendframework.core.Store;
import com.legendframework.core.aop.AbstractProxy;
import com.legendframework.core.ioc.CommonBeansGenerate;
import com.legendframework.core.ioc.annotation.Event;
import com.legendframework.core.ioc.exception.BeansException;
import com.legendframework.core.AbstractLegendPlugin;
import com.legendframework.core.support.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;

import java.lang.annotation.Annotation;

/**
 * @see com.legendframework.core.ioc.annotation.Event 方式的Bean注入类型
 */
public class EventBeansGenerate extends CommonBeansGenerate {

    public EventBeansGenerate(Store store) {
        super(store);
    }

    @Override
    public String getBeanName(Class<?> cls) {
        if (!Listener.class.isAssignableFrom(cls)) {
            throw new BeansException("这个类: '"+cls.getName()+"'需要是: '"+Listener.class.getName()+"' 的子类");
        }
        return StringUtils.isEmpty(cls.getAnnotation(Event.class).value()) ? cls.getName() : cls.getAnnotation(Event.class).value();
    }

    @Override
    public Class<? extends Annotation> getAnnotation() {
        return Event.class;
    }

    @Override
    public void successHandle(String beanName, Object bean , AbstractLegendPlugin plugin) {
        Bukkit.getServer().getPluginManager().registerEvents((Listener) bean,plugin);
    }

    @Override
    protected AbstractProxy getProxyAop() {
        return null;
    }
}
